
#include "led.h"
#include "gpio.h"
#include "FreeRTOS.h"
#include "app_task.h"


void led_set(uint8_t red,uint8_t green ,uint8_t blue)
{
	
	__disable_irq();
	//taskENTER_CRITICAL();
	for(uint8_t j=0;j<28;j++)
	{
		// G
		for (uint8_t i=0;i<=7;i++)
		{
			if ((green & (0x80>>i))>=1)
			{
				ledbit1();
			}
			else
			{
				ledbit0();
			}
		}
		//  R
		for (uint8_t i=0;i<=7;i++)
		{
			if ((red & (0x80>>i))>=1)
			{
				ledbit1();
			}
			
			else
			{
				ledbit0();
			}
		}
		// B 
		for (uint8_t i=0;i<=7;i++)
		{
			if ((blue & (0x80>>i))>=1)
			{
				ledbit1();
			}
			else
			{
				ledbit0();
			}
		}
	}
	DDL_DelayUS(100);
	
	__enable_irq();
	//taskEXIT_CRITICAL();
}

void ledcontralred(void)
{
	ledchar_red();
	ledchar_red();
	ledchar_red();
	ledchar_red();
	ledchar_red();  
	
	ledchar_red();
	ledchar_red();
	ledchar_red();
	ledchar_red();
	ledchar_red(); 
	
	
	ledchar_red();
	ledchar_red();
	ledchar_red();
	ledchar_red();
	ledchar_red();  
	
	ledchar_red();
	ledchar_red();
	ledchar_red();
	ledchar_red();
	ledchar_red();  
	
	ledchar_red();
	ledchar_red();
	ledchar_red();
	ledchar_red();
	ledchar_red(); 
	
	ledchar_red();
	ledchar_red();
	ledchar_red(); 
	
	DDL_DelayUS(100);
}
 
void ledcontralgreen(void)
{
	ledchar_green();
	ledchar_green();
	ledchar_green();
	ledchar_green(); 
	ledchar_green(); 
	
	ledchar_green();
	ledchar_green();
	ledchar_green();
	ledchar_green(); 
	ledchar_green(); 
	
	ledchar_green();
	ledchar_green();
	ledchar_green();
	ledchar_green(); 
	ledchar_green(); 
	
	ledchar_green();
	ledchar_green();
	ledchar_green();
	ledchar_green(); 
	ledchar_green(); 
	
	ledchar_green();
	ledchar_green();
	ledchar_green();
	ledchar_green(); 
	ledchar_green(); 
	
	ledchar_green();
	ledchar_green(); 
	ledchar_green(); 

	DDL_DelayUS(100);
}

void ledcontralblue(void)
{
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 
	
	ledchar_blue(); 
	ledchar_blue(); 
	ledchar_blue(); 

	DDL_DelayUS(100);
}
 

void ledcontralblack(void)
{
	taskENTER_CRITICAL();
	ledchar_black();
	ledchar_black();
	ledchar_black();
	ledchar_black();
	ledchar_black();
	
	ledchar_black();
	ledchar_black();
	ledchar_black();
	ledchar_black();
	ledchar_black();
	
	ledchar_black();
	ledchar_black();
	ledchar_black();
	ledchar_black();
	ledchar_black();
	
	ledchar_black();
	ledchar_black();
	ledchar_black();
	ledchar_black();
	ledchar_black();
	
	ledchar_black();
	ledchar_black();
	ledchar_black();
	ledchar_black();
	ledchar_black();
	
	ledchar_black();
	ledchar_black();
	ledchar_black();
	DDL_DelayUS(100);
	taskEXIT_CRITICAL();
}


void ledchar_green(void)
{
	ledbit1();	
	ledbit1();
	ledbit1();
	ledbit1();
	ledbit1();
	ledbit1();
	ledbit1();
	ledbit1();

	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();

	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
}

void ledchar_red(void)
{
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	
	ledbit1();	
	ledbit1();
	ledbit1();
	ledbit1();
	ledbit1();
	ledbit1();
	ledbit1();
	ledbit1();


	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
}


void ledchar_blue(void)
{
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();

	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	
	ledbit1();	
	ledbit1();
	ledbit1();
	ledbit1();
	ledbit1();
	ledbit1();
	ledbit1();
	ledbit1();
}
void ledchar_black(void)
{
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();

	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
	ledbit0();
}

void ledbit0(void)
{ 
	LED_H;

	__ASM("nop");	
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");		
	__ASM("nop");
	__ASM("nop");
	
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	
	__ASM("nop");
	__ASM("nop");

	LED_L;	
	
	for(uint8_t i=0;i<25;i++)
	{
		__ASM("nop"); 
	}
}

void ledbit1(void)
{
	LED_H;

	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");		
	__ASM("nop");
	__ASM("nop");
	
	
	__ASM("nop");	
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");		
	__ASM("nop");
	__ASM("nop");

	__ASM("nop");	
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");		
	__ASM("nop");
	__ASM("nop");

	__ASM("nop");	
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");		
	__ASM("nop");
	__ASM("nop");

	__ASM("nop");	
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");		
	__ASM("nop");
	__ASM("nop");
	

	__ASM("nop");	
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");		
	__ASM("nop");
	__ASM("nop");

	__ASM("nop");	
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");		
	__ASM("nop");
	__ASM("nop");
	
	__ASM("nop");	
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");		
	__ASM("nop");
	__ASM("nop");
	
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");		
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");
	__ASM("nop");		
	__ASM("nop");
	
	LED_L;	
	for(uint8_t i=0;i<25;i++)
	{
		__ASM("nop"); 
	}
}


